
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Text;
 using System.Windows.Forms;
     public partial class frmDM_DANGNHAP : Form
     {
         public string ma = "";//PK_: giá trị này dc truyền từ bảng danh sách, trường hợp EDIT
         DataTable tbMainDS = new DataTable(); 
        DataTable tbMain = new DataTable(); 
        Boolean iloadform = false;

       #region"    frmDM_DANGNHAP"
        public frmDM_DANGNHAP()
         {
             InitializeComponent();
         }
        #endregion 

       #region"    frmDM_DANGNHAP_Load"
        public void frmDM_DANGNHAP_Load(object sender, EventArgs e) 
         {   
            this.showcombobox();  
             this.showdataDS();  
             iloadform = true;

        }  
        #endregion 

       #region"    các hàm load dữ liệu"
       #region"    showdata"
        void showdata()   
         {  
            
 clsData oclsData = new clsData();//lớp truy xuat du lieu 
 tbMain = oclsData.ExecuteSPGet("sp_DM_DANGNHAP", 
					new string[] {"@flag", "@OBJID"}, 
					new object[] {4, ma}); 
   
         }  
        #endregion 
       #region"    showdataDS"
        void showdataDS()   
         {  
            string[] param = new string[50];    
object[] value = new object[50];    
int ipos = 0;  
param[ipos ] = "@flag"; 
value[ipos++]= 4; 
    string scompare = ""; 
#region "  Search" 
    if(!btnDStimkiem.Enabled) 
    foreach (Control ctr in tbpDanhSach.Controls) 
    { 
        if (ctr.Tag + "" != "" && scompare.IndexOf("." + ctr.Tag + ".") == -1) 
        { 
            string s1 = ctr + ""; 
                param[ipos] = "@" + ctr.Tag + ""; 
            if (s1.IndexOf("TextBox") > 0) 
            { 
                TextBox txt1 = (TextBox)ctr; 
                if (txt1.Text.Trim() != "") 
                    value[ipos++] = txt1.Text.Trim(); 
            } 
            else if (s1.IndexOf("DateTimePicker") > 0) 
            { 
                DateTimePicker dtp1 = (DateTimePicker)ctr; 
                if (dtp1.Checked) 
                    value[ipos++] = dtp1.Value; 
            } 
            else if (s1.IndexOf("ComboBox") > 0) 
            { 
                ComboBox cbo1 = (ComboBox)ctr; 
                if (cbo1.SelectedIndex >= 0 && cbo1.SelectedItem != null) 
                    value[ipos++] = cbo1.SelectedValue; 
            }// 
            else if (s1.IndexOf("CheckBox") > 0) 
            { 
                CheckBox chk1 = (CheckBox)ctr; 
                value[ipos++] = chk1.Checked; 
            } 
            else if (s1.IndexOf("NumericUpDown") > 0) 
            { 
                NumericUpDown num1 = (NumericUpDown)ctr; 
                if (num1.Value > 0) 
                    value[ipos++] = num1.Value; 
            } 
            if (value[ipos] + "" == "")
                 param[ipos] = null;        } 
    } 
#endregion 
grdDM_DANGNHAP.AutoGenerateColumns = false; 
 
 clsData oclsData = new clsData();//lớp truy xuat du lieu 
 tbMainDS = oclsData.ExecuteSPGet("sp_DM_DANGNHAP", param, value); 
 grdDM_DANGNHAP.DataSource = tbMainDS; 
 grdDM_DANGNHAP.Refresh(); 
   
         }  
        #endregion 
       #region"    showinfo"
        void showinfo(int i1edt2sch)   
         {  
            if(btnDStimkiem.Enabled) 
               this.showdata();    
           if (tbMain.Rows.Count == 0) 
{
txtOBJCODE.Text = "";
txtOBJNAME.Text = "";
txtUSERNAME.Text = "";
txtPASSWORD.Text = "";
txtMA_NHANVIEN.Text = "";
dtpTU_GIO.Value = DateTime.Now;
dtpTU_GIO.Checked = false;
dtpDEN_GIO.Value = DateTime.Now;
dtpDEN_GIO.Checked = false;
txtGHI_CHU.Text = "";
return;
}
txtOBJCODE.Text = tbMain.Rows[0]["OBJCODE"] +"";
txtOBJNAME.Text = tbMain.Rows[0]["OBJNAME"] +"";
txtUSERNAME.Text = tbMain.Rows[0]["USERNAME"] +"";
txtPASSWORD.Text = tbMain.Rows[0]["PASSWORD"] +"";
txtMA_NHANVIEN.Text = tbMain.Rows[0]["MA_NHANVIEN"] +"";
dtpTU_GIO.Checked = tbMain.Rows[0]["TU_GIO"] +"" != "" ;
if(dtpTU_GIO.Checked)
	dtpTU_GIO.Text = tbMain.Rows[0]["TU_GIO"] +"";
dtpDEN_GIO.Checked = tbMain.Rows[0]["DEN_GIO"] +"" != "" ;
if(dtpDEN_GIO.Checked)
	dtpDEN_GIO.Text = tbMain.Rows[0]["DEN_GIO"] +"";
txtGHI_CHU.Text = tbMain.Rows[0]["GHI_CHU"] +"";
  
         }  
        #endregion 
       #region"    showcombobox"
        void showcombobox()   
         {  
            
clsData oclsData = new clsData();

  
         }  
        #endregion 
       #endregion 

       #region"    các sự kiện _Click của các nút THÊM - SỬA- TÌM KIẾM - THOÁT - tbpDanhSach"
       #region"    btnDSthem_Click"
       private void btnDSthem_Click(object sender, EventArgs e)
       {
           tbMain = tbMainDS.Copy();   
tbMain.Clear(); 
ma = "-1";    
this.showinfo(1);   
this.setStatusButton(1);    
       }

       #endregion 
       #region"    btnDSthoat_Click"
       private void btnDSthoat_Click(object sender, EventArgs e)
       {
           if (MessageBox.Show("Bạn muốn thoát?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.OK)
           {
                	this.Dispose();
            }
            
}
       #endregion 
       #region"    btnDStimkiem_Click"
       private void btnDStimkiem_Click(object sender, EventArgs e)
       {
           iloadform = false;
tbMain = tbMainDS.Copy();    
tbMain.Clear(); 
 ma = "-1"; 
this.setStatusButton(3); 
this.showinfo(2);    
iloadform = true; 
       
}
       #endregion 
       #endregion 

       #region"    các sự kiện _Click của các nút LƯU - XÓA - SỬA  "
       #region"    tao_ma_tu_dong"
       void tao_ma_tu_dong()
       {
                  }

       #endregion 
       #region"    btnluu_Click"
       private void btnluu_Click(object sender, EventArgs e)
       {
           if (this.save() > 0) 
{
	 MessageBox.Show("Lưu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.showdataDS();
}
       }

       #endregion 
       #region"    save"
       int save()
       {
           int _iReasult = -1;     
if (this.ValidateSave())    
{   
    clsData oclsData = new clsData();//lớp truy xuat du lieu
_iReasult = oclsData.ExecuteSPUpdate("sp_DM_DANGNHAP", 
	 new string[] { "@flag", "@OBJID", "@OBJCODE", "@OBJNAME", "@USERNAME", "@PASSWORD", "@MA_NHANVIEN", "@TU_GIO", "@DEN_GIO", "@GHI_CHU"}, 
	 new object[] { 1, ma, txtOBJCODE.Text, txtOBJNAME.Text, txtUSERNAME.Text, txtPASSWORD.Text, txtMA_NHANVIEN.Text, dtpTU_GIO.Checked? (object)dtpTU_GIO.Value : null, dtpDEN_GIO.Checked? (object)dtpDEN_GIO.Value : null, txtGHI_CHU.Text}); 
}   
return _iReasult;       }

       #endregion 
       #region"    ValidateSave"
       Boolean ValidateSave()
       {
               return true;
       }

       #endregion 
       #region"    delete"
       int delete()
       {
           clsData oclsData = new clsData();//lớp truy xuat du lieu    
int _iReasult = oclsData.ExecuteSPUpdate("sp_DM_DANGNHAP", 
                		 new string[] { "@flag", "@OBJID" },  
                		 new object[] { 3, ma, });  
return _iReasult;
       }

       #endregion 
       #region"    btnxoa_Click"
       private void btnxoa_Click(object sender, EventArgs e)
       {
           if (MessageBox.Show("Bạn muốn xóa OBJID này?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
{   
        if (this.delete() > 0)  
        {  
            MessageBox.Show("Xóa OBJID thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); 
            ma = "-1";    
            this.showdataDS();  
            this.setStatusButton(4);   
        }  
}   
       }

       #endregion 
       #region"    sự kiên chọn trên lưới -- tblDanhSach"
       #region"    showSearch"
       void showSearch(object sender, EventArgs e)
       {
           if (sender != null)
           {
               tbMain = (DataTable)sender;
               grdDM_DANGNHAP.DataSource = tbMain;
               grdDM_DANGNHAP.Refresh();
           }
       }
       #endregion 
       #region"    frmDM_DANGNHAP_KeyDown" 
private void frmDM_DANGNHAP_KeyDown(object sender, KeyEventArgs e) 
{ 
    if (e.Modifiers == Keys.Control && e.KeyCode == Keys.T) 
    { 
        this.btnDSthem_Click(null, null); 
    } 
    else if (e.Modifiers == Keys.Control && e.KeyCode == Keys.K) 
    { 
        this.btnDStimkiem_Click(null, null); 
    } 
    else if (e.KeyCode == Keys.Escape) 
    { 
        this.btnDSthoat_Click(null, null); 
    } 
    else if (e.Modifiers == Keys.Control && e.KeyCode==Keys.L)   
       {   
               this.btnluu_Click(null, null);  
       }   
       else if (e.Modifiers == Keys.Control && e.KeyCode == Keys.X)    
       {   
               this.btnxoa_Click(null, null);  
       }   
} 
#endregion 
       #region"    grdDM_DANGNHAP_SelectionChanged"
       private void grdDM_DANGNHAP_SelectionChanged(object sender, EventArgs e)
       {

 if(iloadform && grdDM_DANGNHAP.CurrentRow != null)
{   if (grdDM_DANGNHAP.CurrentRow.Index >= 0) 
    { 
         iloadform = false; 
         ma = "-1"; 
        string _iSTT = grdDM_DANGNHAP.Rows[grdDM_DANGNHAP.CurrentRow.Index].Cells[0].Value + ""; 
        DataRow[] _dr = tbMainDS.Select("STT='" + _iSTT + "'"); 
        if (_dr.Length > 0) 
        { 
             ma = _dr[0]["OBJID"] + ""; 
             this.setStatusButton(2);    
        } 
             this.showinfo(1);
         iloadform = true; 
    } 
}       }
       private void grdDM_DANGNHAP_DoubleClick(object sender, EventArgs e)
       {
           this.grdDM_DANGNHAP_SelectionChanged(null, null);    
       }
       #endregion 
       #endregion 
       #region "    setStatusButton"
       void setStatusButton(int i1New2Edt3Sch4Del5Sav)    
       {   
           btnDSthem.Enabled = i1New2Edt3Sch4Del5Sav != 1; 
           btnxoa.Enabled = (i1New2Edt3Sch4Del5Sav != 1 && i1New2Edt3Sch4Del5Sav != 4 && i1New2Edt3Sch4Del5Sav != 3);  
           btnDStimkiem.Enabled = i1New2Edt3Sch4Del5Sav != 3;  
           btnluu.Enabled = i1New2Edt3Sch4Del5Sav != 5;    
       }   
       #endregion 
       #region "  txt_KeyDown "
       private void txt_KeyDown(object sender, KeyEventArgs e)
       {   
           if (e.KeyCode == Keys.Enter ||e.KeyCode == Keys.Tab) 
               this.cbo_SelectedIndexChanged(null, null);  
       }   
       #endregion 
       #region "  cbo_SelectionChanged "
       private void cbo_SelectedIndexChanged(object sender, EventArgs e)
       {   
           if (iloadform)  
           {   
               if (!btnDStimkiem.Enabled)  
               {   
                                      iloadform = false;
                  this.showdataDS();
                   iloadform = true;
               }   
               else    
               {   
                   
               }   
           }   
       }   
       #endregion 
       #region"    grdDM_DANGNHAP_DataError"
       private void grdDM_DANGNHAP_DataError(object sender, DataGridViewDataErrorEventArgs e)
       {
           	 string sError = e.Exception.Message; 
       }
       #endregion 
       #region"    grd_DataError"
       private void grd_DataError(object sender, DataGridViewDataErrorEventArgs e)
       {
           	 string sError = e.Exception.Message; 
       }
       #endregion 
   #endregion 

       
}

